package com.yunhuajinfu.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.View;

import com.yunhuajinfu.R;
import com.yunhuajinfu.util.DensityUtil;

/**
 * Created by ekikousei易皇星 on 16/11/16.
 * E-mail:13764664731@163.com
 * Signature:缘分是本书,翻的不经意会错过,读的太认真会流泪!!
 * <p/>
 * TODO:类描述:  带动画的效果的进度条
 */
public class CircleProgressViews extends View {
    //需要执行动画的参数名
    private static final String PROGRESS_PROPERTY = "progress";

    private Paint mmCirPaint;
    private Paint mCirPaint;
    private Paint mArcPaint;
    private Paint mTextPaint;
    //
    private Paint mTextCenterPaint;
    private Paint mTextTopPaint;
    private Paint mTextBottmPaint;
    //
    private int mTextCenterSize;
    private int mTextTopSize;
    private int mTextBottmSize;


    private float radius = 200;
    private int textsize = 10;
    private int progress;
    private int stokeWidth = 10;
    private int circleColor = Color.GRAY;
    private int ccircleColor = Color.WHITE;

    private int arcColor = Color.GREEN;
    private int textColor = Color.BLACK;
    private int speed = 10;

    //
    private String centerText;
    private String topText;
    private String bottomText;

    private  Context mContext;
    public CircleProgressViews(Context context) {
        super(context);
        this.mContext=context;
    }

    public CircleProgressViews(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext=context;
    }

    public CircleProgressViews(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext=context;
    }

    public void setRadius(float radius) {
        this.radius = radius;
        invalidate();
    }

    public void setTextSize(int textsize) {
        this.textsize = textsize;
        invalidate();
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

    public void setStokewidth(int stokeWidth) {
        this.stokeWidth = stokeWidth;
        invalidate();
    }

    public void setColor(int circleColor, int arcColor, int textColor) {
        this.circleColor = circleColor;
        this.arcColor = arcColor;
        this.textColor = textColor;
        invalidate();
    }

    public void setCenterText(String centerText) {
        this.centerText = centerText;
        invalidate();
    }

    public void setTopText(String topText) {
        this.topText = topText;
        invalidate();
    }

    public void setBottomText(String bottomText) {
        this.bottomText = bottomText;
        invalidate();

    }

    public void setmTextCenterSize(int mTextCenterSize) {
        this.mTextCenterSize = mTextCenterSize;
        invalidate();
    }

    public void setmTextTopSize(int mTextTopSize) {
        this.mTextTopSize = mTextTopSize;
        invalidate();
    }

    public void setmTextBottmSize(int mTextBottmSize) {
        this.mTextBottmSize = mTextBottmSize;
        invalidate();
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    private void init() {
        //画最外层圆
        mCirPaint = new Paint();
        mCirPaint.setColor(circleColor);
        mCirPaint.setAntiAlias(true);
        mCirPaint.setStyle(Paint.Style.STROKE);
        mCirPaint.setStrokeWidth(stokeWidth);
//
        //画红色圆
        mArcPaint = new Paint();
        mArcPaint.setColor(arcColor);
        mArcPaint.setAntiAlias(true);
        mArcPaint.setStyle(Paint.Style.STROKE);
        mArcPaint.setStrokeWidth(stokeWidth);
        mArcPaint.setStrokeCap(Paint.Cap.ROUND); //设置圆角

        mTextPaint = new Paint();
        mTextPaint.setColor(getResources().getColor(R.color.bule));
        mTextPaint.setTextSize(textsize);
        mTextPaint.setAntiAlias(true);

        // 小圆
        mmCirPaint = new Paint();
        mmCirPaint.setColor(ccircleColor);
        mmCirPaint.setAntiAlias(true);
        mmCirPaint.setStyle(Paint.Style.STROKE);
        mmCirPaint.setStrokeWidth(stokeWidth);

//字体画笔
        mTextCenterPaint = new Paint();
        mTextCenterPaint.setColor(getResources().getColor(R.color.bule));
        mTextCenterPaint.setTextSize(textsize);
        mTextCenterPaint.setAntiAlias(true);
//
        mTextTopPaint = new Paint();
        mTextTopPaint.setColor(getResources().getColor(R.color.bule));
        mTextTopPaint.setTextSize(mTextTopSize);
        mTextTopPaint.setAntiAlias(true);
//
        mTextBottmPaint = new Paint();
        mTextBottmPaint.setColor(getResources().getColor(R.color.blue_2));
        mTextBottmPaint.setTextSize(mTextBottmSize);
        mTextBottmPaint.setAntiAlias(true);

    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        init();

        float centerX = getWidth() / 2;
        float centerY = getHeight() / 2;
        radius = centerX-stokeWidth/2;
        canvas.drawCircle(centerX, centerY, radius, mCirPaint);
        //画红色的圆
        RectF rectF=new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
        canvas.drawArc(rectF, -90, progress * 360 / 100, false, mArcPaint);
//        canvas.drawArc(centerX - radius, centerY - radius, centerX + radius, centerY + radius,-90, progress * 360 / 100, false, mArcPaint);
        //这个方法就不行了，会报
//        java.lang.NoSuchMethodError: android.graphics.Canvas.drawArc
//        这个方法是 level 21 才加入的，低版本的手机没有这个方法，低版本使用这个方法
        //使用兼容版RectF
//        drawArc (RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)

        //画白色圆圈
        canvas.drawCircle(centerX, centerY - radius, 1, mmCirPaint);
        //设置中心数字显示进度
//       canvas.drawText(progress+"%",centerX-(mTextPaint.measureText(progress+"%"))/2,centerY+textsize/2,mTextPaint);
        //设置中心文本
        if (null != centerText) {
            canvas.drawText(centerText, centerX - (mTextCenterPaint.measureText(centerText)) / 2, centerY + textsize / 2, mTextCenterPaint);
        }
        if (null != topText) {
            //上方文字
            canvas.drawText(topText, centerX - (mTextTopPaint.measureText(topText)) / 2, centerY - textsize, mTextTopPaint);

        }
        if (null != bottomText) {
            //下方文字
            canvas.drawText(bottomText, centerX - (mTextBottmPaint.measureText(bottomText)) / 2, centerY  + DensityUtil.dip2px(mContext,40), mTextBottmPaint);
        }

    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (speed != 0) {
            startProgress();
        }
    }

    public void startProgress() {
        final int preProgress = progress;
        new CountDownTimer(preProgress * speed, speed) {
            @Override
            public void onTick(long l) {
                    setProgress(preProgress - (int) (l / speed));
                    invalidate();
            }

            @Override
            public void onFinish() {
                    setProgress(preProgress);
                    invalidate();
                    this.cancel();
            }
        }.start();

//        ValueAnimator animator = ValueAnimator.ofInt(0, progress);
//        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                int progress = (int) animation.getAnimatedValue();
//                setProgress(progress);
//                invalidate();
//
//            }
//        });
//        animator.setRepeatCount(0);
//        animator.setDuration(2000);
//        animator.start();
    }
}
